import { useState } from "react";
import { addMessageApi } from "~/api/message";

interface AddMessageParams {
    type: number;
    title?: string;
    content?: string;
    user_id?: string;
}

export const useAddMessage = () => {
    const [loading, setLoading] = useState(false);
    const [error, setError] = useState<string | null>(null);
    const addMessage = async (params: AddMessageParams) => {
        setLoading(true);
        setError(null);
        try {
            const response = await addMessageApi(params);
            return response;
        } catch (err: any) {
            setError(err.message || "请求失败");
            throw err;
        } finally {
            setLoading(false);
        }
    };
    return { addMessage, loading, error };
};

